var gf = require('gfportal'),
    config = gf.config,
    routes = gf.controllers,
    http_forwarder = routes.http_forwarder,
    http_forwarder_hs = routes.http_forwarder_hs,
    logging_middleware = gf.services.logging_middleware,
    add_route = gf.lib.add_route;

var url_defs = [];

function statistic_js(req, res) {
    res.set('Content-Type', 'text/javascript');
    var patterns = [];
    for (var i = 0; i < url_defs.length; i++) {
        var p = url_defs[i][1];
        p = p.replace(/:\w+/, "\\w+");
        p = new RegExp("^" + p + "$");
        patterns.push(p.source);
    }
    res.render('statistic_js', {
        patterns: patterns,
        userAgent: req.header('User-Agent'),
        host: req.host
    });
}



module.exports = function(app) {
    url_defs.push(["get", "/pv/*",
        function(req, res) {
            return res.json(1);
        }
    ]);

    //用户管理
    url_defs.push(['get', '/user/list', routes.admin_user, routes.user_list]);
    url_defs.push(['get', '/user/role/list', routes.admin_user, routes.user_role_list]);
    url_defs.push(['get', '/user/get_login_url', routes.user_get_login_url]);
    url_defs.push(['get', '/user/logout', routes.user_logout]);
    url_defs.push(['get', '/user/get_redirect_info', routes.user_get_redirect_info]);
    //赋予栏目权限
    url_defs.push(['post', '/user/auth_category', routes.admin_user, routes.user_auth_category]);

    //资源管理
    url_defs.push(['get', '/res/list', routes.res_list]);
    url_defs.push(['post', '/res/add', routes.res_add]);
    url_defs.push(['get', '/res/tree', routes.res_tree]);
    url_defs.push(['get', '/res/detail', routes.res_detail]);
    url_defs.push(['post', '/res/edit', routes.res_edit]);
    url_defs.push(['post', '/res/del', routes.res_del]);
    url_defs.push(['get', '/res/authed/list', routes.res_authed_list]);

    //用来管理数据字典
    url_defs.push(['get', "/dict/list", routes.dict_list]);
    url_defs.push(['post', "/dict/add", routes.admin_user, routes.dict_add]);
    url_defs.push(['post', "/dict/edit", routes.admin_user, routes.dict_edit]);
    url_defs.push(['get', "/dict/tree", routes.dict_tree]);
    url_defs.push(['post', "/dict/del", routes.dict_del]);
    url_defs.push(['get', "/dict/detail", routes.dict_detail]);
    url_defs.push(['get', "/dict/tree_ext", routes.dicts_tree_ext]);
    url_defs.push(['get', "/dict/list_simple", routes.dict_list_simple]);

    //角色管理
    url_defs.push(['get', '/role/list', routes.role_list]);
    url_defs.push(['get', '/role/tree', routes.role_tree]);
    url_defs.push(['post', '/role/add', routes.role_add]);
    url_defs.push(['get', '/role/detail', routes.role_detail]);
    url_defs.push(['post', '/role/edit', routes.role_edit]);
    url_defs.push(['post', '/role/del', routes.role_del]);
    url_defs.push(['get', '/role/auth', routes.role_auth]);
    url_defs.push(['post', '/role/auth/save', routes.role_auth_save]);
    url_defs.push(['get', '/role/user/list', routes.role_user_list]);
    url_defs.push(['post', '/role/user/save', routes.role_user_save]);
    url_defs.push(['post', '/role/user/del', routes.role_user_del]);
    //文章资讯管理
    url_defs.push(['get', "/article/list", routes.article_list]);
    url_defs.push(['get', "/article/web/list", routes.article_web_list]);
    url_defs.push(['get', "/article/web/recommend", routes.article_web_command]);
    url_defs.push(['post', "/article/add", routes.article_add]);
    url_defs.push(['get', "/article/detail", routes.article_detail]);
    url_defs.push(['post', "/article/del", routes.admin_user, routes.article_del]);
    url_defs.push(['post', "/article/edit", routes.admin_user, routes.article_edit]);
    url_defs.push(['post', "/article/add/attach", routes.admin_user, routes.article_add_attach]);
    url_defs.push(['post', "/article/del/attach", routes.admin_user, routes.article_del_attach]);
    //文件管理
    url_defs.push(["post", "/file/upload", routes.admin_user, routes.file_upload]);
    url_defs.push(["post", "/file/upload2", routes.admin_user, routes.file_upload_2]);
    url_defs.push(["post", "/file/common-upload", routes.admin_user, routes.file_common_upload]);
    url_defs.push(["post", "/file/upload_delete", routes.admin_user, routes.file_upload_delete]);
    url_defs.push(["post", "/file/uploadck", routes.file_uploadck]);
    url_defs.push(["get", "/file/download", routes.file_download]);
    url_defs.push(["get", "/file/download/:file_id", routes.file_download]);
    //表单管理
    url_defs.push(['get', '/form/list', routes.form_list]);
    url_defs.push(['post', '/form/add', routes.form_add]);
    url_defs.push(['get', '/form/detail', routes.form_detail]);
    url_defs.push(['post', '/form/edit', routes.admin_user, routes.form_edit]);
    url_defs.push(['post', '/form/del', routes.admin_user, routes.form_del]);
    url_defs.push(['get', '/form/field/list', routes.form_field_list]);
    url_defs.push(['post', '/form/field/add', routes.form_field_add]);
    url_defs.push(['get', '/form/field/detail', routes.form_field_detail]);
    url_defs.push(['post', '/form/field/edit', routes.form_field_edit]);
    url_defs.push(['post', '/form/field/del', routes.form_field_del]);
    url_defs.push(['get', '/form/:form_code', routes.form_get]);
    url_defs.push(['get', '/form/search/field/list', routes.form_search_field_list]);
    url_defs.push(['post', '/form/search/field/add', routes.form_search_field_add]);
    url_defs.push(['get', '/form/search/field/detail', routes.form_search_field_detail]);
    url_defs.push(['post', '/form/search/field/edit', routes.form_search_field_edit]);
    url_defs.push(['post', '/form/search/field/del', routes.form_search_field_del]);
    //表单操作
    url_defs.push(['post', '/form/common/add', routes.admin_user, routes.form_common_add]);
    url_defs.push(['post', '/form/common/edit', routes.admin_user, routes.form_common_edit]);
    url_defs.push(['post', '/form/common/del', routes.admin_user, routes.form_common_del]);
    url_defs.push(['get', '/form/common/listall', routes.form_common_listall]);
    url_defs.push(['get', '/form/common/detail', routes.form_common_detail]);
    url_defs.push(['get', '/form/common/list', routes.form_common_listpage]);
    //广告管理
    url_defs.push(['post', "/ad/add", routes.admin_user, routes.ad_add]);
    url_defs.push(['post', "/ad/del", routes.admin_user, routes.ad_del]);
    url_defs.push(['post', "/ad/edit", routes.admin_user, routes.ad_edit]);
    url_defs.push(['get', "/ad/list", routes.ad_list_validdate]);
    url_defs.push(['get', "/ad/list/all", routes.ad_list]);
    url_defs.push(['get', "/ad/detail", routes.ad_detail]);
    url_defs.push(['post', "/adv/add", routes.admin_user, routes.ad_add]);
    url_defs.push(['post', "/adv/del", routes.admin_user, routes.ad_del]);
    url_defs.push(['post', "/adv/edit", routes.admin_user, routes.ad_edit]);
    url_defs.push(['get', "/adv/list", routes.ad_list_validdate]);
    url_defs.push(['get', "/adv/list/all", routes.ad_list]);
    url_defs.push(['get', "/adv/detail", routes.ad_detail]);

    url_defs.push(['all', "/login", routes.login_ownpwd_admin]);

    app.get("/render/html", routes.render_precompile);

    for (var i = 0; i < url_defs.length; i++) {
        var method = url_defs[i][0];
        var path = url_defs[i][1];
        var params = [path].concat(url_defs[i].slice(2));
        console.log(path);
        app[method].apply(app, params);
    }

};